package ch.zhaw.swp1.view.component;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * Date picker to select date in GUI
 * @author Daniel Tiefenauer
 */
public class DatePicker extends JPanel {

	/**
	 * Default version 
	 */
	private static final long serialVersionUID = 1L;
	
	private JComboBox<String> dayPicker = new JComboBox<String>();
	private JComboBox<String> monthPicker = new JComboBox<String>();
	private JComboBox<String> yearPicker = new JComboBox<String>();
	private JComboBox<String> hourPicker = new JComboBox<String>();
	private JComboBox<String> minutePicker = new JComboBox<String>();
	
	private Calendar cal = Calendar.getInstance();
	
	/************************************************************************************/
	// Constructors
	/*----------------------------------------------------------------------------------*/
	/**
	 * Default constructor with current date
	 */
	public DatePicker(){
		this(new Date(System.currentTimeMillis()));
	}
	
	/**
	 * Overloaded Constructor specifying a pre-set date
	 * @param date
	 */
	public DatePicker(Date date){
		this.cal.setTime(date);
		cal.set(Calendar.SECOND, 0);
		init();
		layoutComponents();
	}

	/***************************************************************************************/
	// Methods
	/*----------------------------------------------------------------------------------*/
	/**
	 * Initialize Components with correct values
	 */
	private void init(){
		// DayPicker
		for(int i=1; i<=31; i++){
			dayPicker.addItem(""+i);
		}
		
		// Month Picker
		for(int i=1; i<=12; i++){
			monthPicker.addItem(""+i);
		}
		
		// Year Picker
		for(int i=0; i<=100; i++){
			yearPicker.addItem("" + (cal.get(Calendar.YEAR) + i));
		}
		
		// Hour picker
		for(int i=0; i<=23; i++){
			String item = (i<10)?(""+0+i):""+i;
			hourPicker.addItem(item);
		}
		
		// Minute picker
		for(int i=0; i<=59; i++){
			String item = (i<10)?(""+0+i):""+i;
			minutePicker.addItem(item);
		}
		
		// set default values to be shown
		dayPicker.setSelectedIndex(cal.get(Calendar.DATE) -1);
		monthPicker.setSelectedIndex(cal.get(Calendar.MONTH));
		hourPicker.setSelectedIndex(cal.get(Calendar.HOUR_OF_DAY));
		minutePicker.setSelectedIndex(cal.get(Calendar.MINUTE));

		// action listeners
		dayPicker.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				cal.set(Calendar.DATE, Integer.parseInt(dayPicker.getSelectedItem().toString()));
			}
		});
		
		monthPicker.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				cal.set(Calendar.MONTH, Integer.parseInt(monthPicker.getSelectedItem().toString()) -1);
			}
		});
		
		yearPicker.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				cal.set(Calendar.YEAR, Integer.parseInt(yearPicker.getSelectedItem().toString()));
			}
		});
		
		hourPicker.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hourPicker.getSelectedItem().toString()));
			}
		});
		
		minutePicker.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				cal.set(Calendar.MINUTE, Integer.parseInt(minutePicker.getSelectedItem().toString()));
			}
		});
	}
	
	/**
	 * Lay out components on screen
	 */
	private void layoutComponents(){
		add(new JLabel("Tag:"));
		add(dayPicker);
		add(new JLabel("Monat:"));
		add(monthPicker);
		add(new JLabel("Jahr:"));
		add(yearPicker);
		
		add(new JLabel("Uhrzeit:"));
		add(hourPicker);
		add(new JLabel(":"));
		add(minutePicker);
	}

	/**
	 * Set datePicker as active or not active
	 * @param editable true if the picker should be active, false if not
	 */
	public void setActive(boolean editable){
		monthPicker.setEnabled(editable);
		dayPicker.setEnabled(editable);
		yearPicker.setEnabled(editable);
		hourPicker.setEnabled(editable);
		minutePicker.setEnabled(editable);
		setEnabled(editable);
	}
	
	/**
	 * Reset Datepicker with default values and current date
	 */
	public void reset(){
		cal.setTime(new Date(System.currentTimeMillis()));
		init();
	}
	
	/***************************************************************************************/
	// Getter/Setter
	/*----------------------------------------------------------------------------------*/
	public Date getDate(){
		if(isEnabled()){
			cal.set(Calendar.SECOND, 0);
			return cal.getTime();
		}
		return null;
	}


}
